//
//  WSPlayerViewController.m
//  RunningMan
//
//  Created by 王帅 on 16/5/23.
//  Copyright © 2016年 WSJ. All rights reserved.
//

#import "WSPlayerViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "WSPlayerTool.h"
#import "WSMusicModel.h"
#import "WSMyTool.h"
#import "WSPlayerView.h"
@interface WSPlayerViewController ()<PlayerToolDeletage>
@property (nonatomic ,assign) NSInteger currenCount;

@property (nonatomic, strong) WSPlayerView *playerView;

@end

@implementation WSPlayerViewController

static AVAudioPlayer *player;

+(id)sharePlayMusic {
    static WSPlayerViewController *play = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        play = [[WSPlayerViewController alloc] init];
        
    });
    return play;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.playerView = [[WSPlayerView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.playerView.btnLastMusic addTarget:self action:@selector(lastMusicBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.playerView.btnNextMusic addTarget:self action:@selector(nextMuscBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.playerView];
    
    
    [WSPlayerTool shareMusic].delegate = self;
    self.currenCount = -1;
    
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self playCurrentMusic];
    [self.playerView createAnimation];
}
//当前播放的歌曲
-(void)playCurrentMusic {
    if (self.index == self.currenCount) {
        return;
    } else {
        self.currenCount = self.index;
        WSMusicModel *model = self.arrayMusic[self.index];
        self.playerView.labelSongName.text = model.songname;
        self.playerView.labelSonger.text = model.songer;
        [self.playerView.imageViewPhoto sd_setImageWithURL:[NSURL URLWithString:model.thumbnail_url] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
        [[WSPlayerTool shareMusic ] preparePlayMusicWithUrl:model.filename];
    }
}
#pragma mark 两个代理方法
- (void)playMusicCurrenTime:(NSString *)currenTime totalTime:(NSString *)totalTime Progress:(CGFloat)progress{
    self.playerView.labelTime.text = [NSString stringWithFormat:@"%@ / %@", currenTime, totalTime];
    self.playerView.progress.progress = progress;
//    [self.playerView.imageViewPhoto setTransform:CGAffineTransformRotate(self.playerView.imageViewPhoto.transform, M_PI/20)];


}
- (void)automaticPlayNextMusic {
    
    NSInteger number = self.arrayMusic.count - 1;
    
    if (self.currenCount == number) {
        
        self.index = 0;
        
    } else {
        self.index++;
        
    }
    
    [self playCurrentMusic];
}



- (void)lastMusicBtnClick:(UIButton *)sender {
        NSInteger number = self.arrayMusic.count - 1;
        if (self.currenCount == 0) {
            self.index = number;
        } else {
            self.index--;
        }
        [self playCurrentMusic];

}
- (void)nextMuscBtnClick:(UIButton *)sender {
        NSInteger number = self.arrayMusic.count - 1;
        if (self.currenCount == number) {
            self.index = 0;
        }  else {
            self.index++;
        }
        [self playCurrentMusic];

}




@end
